package com.trkj.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.mapping.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IBedTableDao;
import com.trkj.dao.jpa.IDepositTableDao;
import com.trkj.dao.jpa.IHospWorkOutNoticeDao;
import com.trkj.dao.jpa.ILiveinrecordTableDao;
import com.trkj.dao.jpa.ILiveoutTableDao;
import com.trkj.dao.mybatis.LiveOutMapper;
import com.trkj.pojos.BedTable;
import com.trkj.pojos.HospWorkOutNotice;
import com.trkj.pojos.LiveinrecordTable;
import com.trkj.pojos.LiveoutTable;

@Service
@Transactional
public class LiveOutService {
	@Autowired
	private LiveOutMapper liveOutMapper;
	@Autowired
	private ILiveinrecordTableDao iLiveinrecordTableDao;
	@Autowired
	private TallyService tallyService;
	@Autowired
	private ILiveoutTableDao iLiveoutTableDao;
	@Autowired
	private IDepositTableDao iDepositTableDao;
	@Autowired
	private IBedTableDao iBedTableDao;
	@Autowired
	private IHospWorkOutNoticeDao iHospWorkOutNoticeDao;
	/**
	 * 根据病人姓名来查询出院通知
	 * @param likeName
	 */
	public List<HospWorkOutNotice> queryLiveOutNoticByLikeName(String likeName){
		return liveOutMapper.queryOutNoticByLikeName(likeName);
	}
	/**
	 * 出院判断
	 */
	public Map<String,Object> decideToLiveOut(String liveID){
		Map<String,Object> map = new HashMap<>();
		
		//判断床位状态
		LiveinrecordTable liveR = iLiveinrecordTableDao.findOne(liveID);
		map.put("tel", liveR.getPatienttel());
		map.put("office", liveR.getLiveoffice4());
		map.put("MZnumner", liveR.getMjnumber3());
		BedTable bedTable = liveR.getBedTable();
		if(bedTable.equals(null)){
			map.put("bedStatus", "已退");
		}else{
			map.put("bedStatus", "未退");
		}
		//判断押金状态
		String DepositIDStatus = iDepositTableDao.queryDepositIDStatus(liveID);
		if(DepositIDStatus.equals("已退")){
			map.put("DepositIDStatus", DepositIDStatus);
		}else{
			map.put("DepositIDStatus", "未退");
		}
		//判断费用状态
		Map<String, Object> queryTallyBalance = tallyService.queryTallyBalance(liveID);
		map.put("alltally", queryTallyBalance.get("alltally"));
		map.put("nopay", queryTallyBalance.get("nopay"));
		
		return map;
	}
	/**
	 * 创建出院号
	 * @return
	 */
	public String createLiveOutID(){
		
		String newLiveOutID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iLiveoutTableDao.queryLiveOutID(LikeID);
		
		if(list.isEmpty()){
			newLiveOutID = "CY" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 newLiveOutID = "CY" + str + nextID;
		}
		return newLiveOutID;
	}
	/**
	 * 出院啦   修改病房状态   处理出院通知状态  清空床位   修改住院登记状态  新建出院登记
	 * @return
	 */
	public boolean liveOut(LiveoutTable liveoutTable){
		
		try {
			LiveinrecordTable liveTable = iLiveinrecordTableDao.findOne(liveoutTable.getLiveid3());//获取住院登记
			
			BedTable bedTable = liveTable.getBedTable();//获取床位  修改状态
			bedTable.setBedstatus("待打扫");
			iBedTableDao.save(bedTable);
			
			liveTable.setLivestatus("已出院");//修改住院登记状态  并清空床位
			liveTable.setBedTable(null);
			
			Long outID = Long.valueOf(liveoutTable.getLiveoutnoticid());//修改出院通知状态
			HospWorkOutNotice workOutNotice = iHospWorkOutNoticeDao.findOne(outID);
			workOutNotice.setOutstatus("已处理");
			iHospWorkOutNoticeDao.save(workOutNotice);
			
			iLiveoutTableDao.save(liveoutTable);//保存出院记录
			return true;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return false;
		}
		
	}

	
}
